//
//  ViewController.m
//  WaDaoServiceDemo
//
//  Created by wulanzhou on 16/6/3.
//  Copyright © 2016年 wulanzhou. All rights reserved.
//

#import "ViewController.h"
#import "HttpManager.h"
#import "SystemUser.h"
#import "uploadFileModel.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)dealloc{

    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    //refresh_token过期，重新登录通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reLogin) name:kNotificationTokenExpiresReLogin object:nil];
    
    
    
    if ([SystemUser shareInstance].isLogin) {
        NSLog(@"表示已登录");
        [self multRequests]; //开始请求数据
    }else{
        //登录
        [self reLogin];
    
    }
    
    //单文件上传调用
    
    uploadFileModel *fileModel=[[uploadFileModel alloc] init];
    fileModel.fileName=@"文件名";
    fileModel.name=@"图片参数名";
    fileModel.imageData=nil; //图片数据
    
    [[HttpManager shareInstance] uploadFileWithMethodName:@"方法名" parameters:nil uploadFile:fileModel progress:^(float progress){
        
        //进度条
        
    } success:^(HttpResponse *response) {
        
        if (response.success) {
            //上传成功
            NSLog(@"上传成功返回数据data =%@",response.responseData);
            return;
        }
        NSLog(@"上传失败状态码 =%@",response.code);
        NSLog(@"上传失败原因 =%@",response.errMsg);
        
    }];

   
    // Do any additional setup after loading the view, typically from a nib.
}

/**
 *  重新登录
 */
- (void)reLogin{
    
    NSLog(@"开始重新登录中。。。。");
    
    //登录请求参数
    NSDictionary *params = @{@"account":@"15868576625",@"password":@"111111"};
    
    
    //登录请求
    [[HttpManager shareInstance] requestWithHttpWayType:SCHW_Get methodName:@"user/login" parameters:params success:^(HttpResponse *response) {
        if (response.success) { //请求成功
            
            
            //保存登录信息
            SystemUser *mod=[[SystemUser alloc] initWithDictionary:response.responseData];
            mod.phone=@"15868576625";
            mod.password=@"111111";
            [mod saveUser];
            
            /**取得登录信息如下所示
             
             [SystemUser shareInstance].userId;
             [SystemUser shareInstance].phone;
             [SystemUser shareInstance].password;
             xxxx
             
             或者
             SystemUser mod=[SystemUser shareInstance];
             mod.userId;
             mod.password;
             xxxxx
             
             **/
            
            NSLog(@"登录请求返回数据data =%@",response.responseData);
            //开始请求数据
            [self multRequests];
            return;
        }
        
        NSLog(@"登录失败状态码 =%@",response.code);
        NSLog(@"登录失败原因 =%@",response.errMsg);
    }];
}

/**
 *  加载商品请求
 */
- (void)multRequests{
    
    NSDictionary *dic=@{@"type":@"hot",@"current_page":@1,@"page_size":@10,@"access_token":[SystemUser shareInstance].access_token};

    [[HttpManager shareInstance] requestWithHttpWayType:SCHW_Get methodName:@"goods/get_list" parameters:dic success:^(HttpResponse *response) {
        NSLog(@"hot取得商品列表完成");
        NSLog(@"code =%@",response.code);
        NSLog(@"errMsg =%@",response.errMsg);
        NSLog(@"responseData =%@",response.responseData);
    }];
    

    
    dic=@{@"type":@"new",@"current_page":@1,@"page_size":@10,@"access_token":[SystemUser shareInstance].access_token};
    
    [[HttpManager shareInstance] requestWithHttpWayType:SCHW_Get methodName:@"goods/get_list" parameters:dic success:^(HttpResponse *response) {
        NSLog(@"new取得商品列表完成");
        NSLog(@"code =%@",response.code);
        NSLog(@"errMsg =%@",response.errMsg);
        NSLog(@"responseData =%@",response.responseData);
    }];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
